package com.liwenfeng.tcpandupd;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;

public class LoginServer {
    List<Socket> acceptSockets = new ArrayList<>();
    public static final String USER_FILE_PATH = "src/main/resources/user.properties";
    Properties properties;

    public LoginServer() {
        properties = new Properties();
        try {
            properties.load(LoginServer.class.getResourceAsStream("/user.properties"));
        } catch (IOException e) {
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        System.out.println("服务端启动----------");
        new LoginServer().server();
    }

    public void server() {
        try (
                ServerSocket socket = new ServerSocket(8888);
        ) {
            while (true) {
                Socket accept = socket.accept();
                System.out.println("建立新连接=" + accept.getRemoteSocketAddress());
                acceptSockets.add(accept);
                CompletableFuture.runAsync(() -> {
                    try (
                            InputStream is = accept.getInputStream();
                            OutputStream os = accept.getOutputStream();
                    ) {
                        while (true) {
                            byte[] bytes = new byte[1024];
                            try {
                                int len = is.read(bytes);
                                String msg = new String(bytes, 0, len);
                                String[] split = msg.split("=");
                                System.out.println("用户名：" + split[0] + "，密码：" + split[1]+"，方法："+split[2]);
                                if (Objects.equals(split[2], "登录")) {
                                    String result = login(split[0], split[1]);
                                    os.write(result.getBytes());
                                } else {
                                    String result = register(split[0], split[1]);
                                    os.write(result.getBytes());
                                }
                            } catch (IOException e) {
                                acceptSockets.remove(accept);
                                System.out.println(accept.getRemoteSocketAddress() + "下线了...");
                                break;
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                });
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String login(String username, String password) {
        if (properties.containsKey(username) && properties.getProperty(username).equals(password)) {
            return "登录成功";
        }else {
            return "用户名或密码错误";
        }
    }

    public String register(String username, String password) throws IOException {
        if (properties.containsKey(username)) {
            return "用户名已存在！不能注册";
        } else {
            properties.setProperty(username, password);
            properties.store(new FileWriter(USER_FILE_PATH), "");
            return "注册成功";
        }
    }
}
